home *** CD-ROM | disk | FTP | other *** search
/ Pascal Super Library / Pascal Super Library (CW International)(1997).bin / BORL_TIP / TI1000 / TI1722.ASC < prev    next >
Text File  |  1993-10-21  |  3KB  |  133 lines

  1.  
  2.  
  3.  
  4.  
  5.  
  6.  
  7.  
  8.   PRODUCT  :  Borland Pascal                        NUMBER  :  1722
  9.   VERSION  :  All
  10.        OS  :  All
  11.      DATE  :  October 21, 1993                         PAGE  :  1/2
  12.  
  13.     TITLE  :  Declaring an array on the heap
  14.  
  15.  
  16.  
  17.  
  18.     The following program, HEAPARY.PAS, demonstrates
  19.     how to place an array on the heap. Programmers
  20.     need to do this when they get errors saying that
  21.     there are "too many variables" or that they are out
  22.     of data space.
  23.  
  24.     The solution to the above errors is simply to
  25.     move an array up on the heap by creating a pointer
  26.     to the array. Notice that the actual definition of
  27.     the array is in the TYPE section, not the VAR
  28.     section. If you write a statement like:
  29.  
  30.     var MyArray: array[0..100] of Char
  31.  
  32.     then you are allocating memory in the data segment,
  33.     which is what you want to avoid. The solution is to
  34.     define the array in the TYPE section, and then
  35.     declare it in the VAR section:
  36.  
  37.     var MyArray: PMyArray;
  38.  
  39.     where PMyArray is a pointer to an array. To see the complete
  40.     code for this type of example, refer to the HEAPARY program
  41.     shown below.
  42.  
  43.     The $X is just so HEAPARY.PAS can use ReadKey
  44.     without returning a result, the FlushKeyBuffer and Pause
  45.     methods are thrown in case you are interested. The body
  46.     of the program and the Type declaration are the parts
  47.     you want to look at. Always remember to dispose memory
  48.     allocated with New!
  49.  
  50.   Program HeapAry;
  51.  
  52.   {$X+}
  53.   Uses
  54.     Dos,
  55.     Crt;
  56.  
  57.   Type
  58.     PMyArray = ^TMyArray;
  59.     TMyArray = array[0..999] of LongInt;
  60.  
  61.  
  62.  
  63.  
  64.  
  65.  
  66.  
  67.  
  68.  
  69.  
  70.  
  71.  
  72.  
  73.  
  74.   PRODUCT  :  Borland Pascal                        NUMBER  :  1722
  75.   VERSION  :  All
  76.        OS  :  All
  77.      DATE  :  October 21, 1993                         PAGE  :  2/2
  78.  
  79.     TITLE  :  Declaring an array on the heap
  80.  
  81.  
  82.  
  83.  
  84.   procedure FlushKeyBuffer;
  85.   var Recpack : registers;
  86.   begin
  87.     with recpack do begin
  88.       Ax := ($0c shl 8) or 6;
  89.       Dx := $00ff;
  90.     end;
  91.     Intr($21,recpack);
  92.   end;
  93.  
  94.   procedure Pause;
  95.   begin
  96.     FlushKeyBuffer;             { Make sure key buffer is empty!  }
  97.     ReadKey;                    { Pause                           }
  98.   end;
  99.  
  100.   var
  101.     MyAry: PMyArray;
  102.     i: Integer;
  103.  
  104.   begin
  105.     ClrScr;
  106.     New(MyAry);                 { Allocate the memory on the heap }
  107.     for i := 0 to 999 do        { Fill out array                  }
  108.       MyAry^[i] := i;
  109.     for i := 500 to 510 do      { Write the array to the screen   }
  110.       WriteLn(MyAry^[i]);
  111.     Pause;
  112.     Dispose(MyAry);             { Dispose memory!                 }
  113.   end.
  114.  
  115.   DISCLAIMER: You have the right to use this technical information
  116.   subject to the terms of the No-Nonsense License Statement that
  117.   you received with the Borland product to which this information
  118.   pertains.
  119.  
  120.  
  121.  
  122.  
  123.  
  124.  
  125.  
  126.  
  127.  
  128.  
  129.  
  130.  
  131.  
  132.  
  133.